package com.javabasic.algorithm.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/6/23 21:55
 */
public class GroupThePeopleGivenTheGroupSizeTheyBelongTo {


    /**
     * 思维逻辑+暴力解法
     * @param groupSizes
     * @return
     */
    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        int len = groupSizes.length;

        Map<Integer, List<Integer>> groupSizeToPeoplesMap = new HashMap<>(16);
        List<Integer> peoples;
        for (int i = 0; i < len; i++) {
            peoples = groupSizeToPeoplesMap.getOrDefault(groupSizes[i], new ArrayList<>());
            peoples.add(i);
            groupSizeToPeoplesMap.put(groupSizes[i], peoples);
        }

        List<List<Integer>> result = new ArrayList<>();
        Set<Map.Entry<Integer, List<Integer>>> entries = groupSizeToPeoplesMap.entrySet();
        int groupSize;
        int groupNums;
        int tagIndex;
        for (Map.Entry<Integer, List<Integer>> entry : entries) {
            groupSize = entry.getKey();
            groupNums = entry.getValue().size() / groupSize;
            tagIndex = 0;
            for (int i = 0; i < groupNums; i++) {
                result.add(new ArrayList<>(entry.getValue().subList(tagIndex, tagIndex + groupSize)));
                tagIndex += groupSize;
            }
        }

        return result;
    }
}
